<?php
/**
 *短信发送类
 *
 */
class SMS
{
	//请求URL
	private $_url;
	
	//企业ID
	private $_user_id;
	
	//发送的用户账号
	private $_account;
	
	//发送账号密码
	private $_password;
	
	//发送的手机
	private $_mobile;
	
	//发送任务命令
	private $_action;
	
	//发送内容
	private $_content;
	
	//是否检查内容包含非法关键字
	private $_checkcontent;
	
	//任务名
	private $_task_name;
	
	//发送时间
	private $_send_time;
	
	//号码总数
	private $_countnumber;
	
	//手机号码数量
	private $_mobilenumber;
	
	//小灵通或者座机号码数量
	private $_telephonenumber;
	
	//短消息中文标签
	private $_msg_tag;
	
	/**
	 *构造函数
	 */
	public function __construct()
	{
		global $SMS_CONF;
		$this->_url 		=	$SMS_CONF['url'];
		$this->_user_id 	= 	$SMS_CONF['user_id'];
		$this->_account 	= 	$SMS_CONF['account'];
		$this->_password 	= 	$SMS_CONF['password'];
		$this->_msg_tag		=	$SMS_CONF['msg_tag'];
	}
	
	/**
	 *发送短信
	 *@param $content 发送短信的内容
	 *		 $mobiles 发送短信的手机号，以数组的形式存放
	 *@param 服务器端返回的xml
	 */
	public function send_com_msg($content, $mobiles)
	{
		$this->_action = 'send';
		$this->_mobile = implode(',', $mobiles);
		$this->_content = $content.$this->_msg_tag;
		//$this->_checkcontent = 1;
		$this->_countnumber = count($mobiles);
		$this->_mobilenumber = count($mobiles);
		$this->_telephonenumber =0;
		$this->_tast_name = '发送聚会邀请函短信';
		return $this->request();
	}
	
	/**
	 *向服务器端发起请求
	 *@param void
	 *@return 请求的返回结果
	 */
	public function request()
	{
		//每次请求的必选数据
		$data = array(
			'action' 	=>	$this->_action,
			'userid' 	=>	$this->_user_id,
			'account'	=>	$this->_account,
			'password'	=>	$this->_password,
		);
		
		//个性化定制的数据
		if(isset($this->_checkcontent))
		{
			$data['checkcontent'] = $this->_checkcontent;
		}
		
		if(isset($this->_mobile))
		{
			$data['mobile'] = $this->_mobile;
		}
		
		if(isset($this->_mobilenumber))
		{
			$data['mobilenumber'] = $this->_mobilenumber;
		}
		
		if(isset($this->_countnumber))
		{
			$data['countnumber'] = $this->_countnumber;
		}
		
		if(isset($this->_telephonenumber))
		{
			$data['telephonenumber'] = $this->_telephonenumber;
		}
		
		if(isset($this->_content))
		{
			$data['content'] = $this->_content;
		}
		
		if(isset($this->_tast_name))
		{
			$data['taskName'] = $this->_tast_name;
		}
		$data['sendTime'] = "";
		
		//发送POST请求，并且返回结果
		//print_r($data);
		
		$tmp = array();
		foreach($data as $param => $content)
		{
			$tmp[]= $param.'='.$content;
		}
		$param_str = implode('&', $tmp);
		
		return $this->do_post_request($this->_url, $param_str);
	}
	
	/**
	 *发送POST请求
	 *@param $url 请求的URL
	 *		 $data 发送的数据，以数组的形式
	 *		 $optional_headers 发送的头部信息
	 */
	private function do_post_request($url, $data, $optional_headers = null)
	{
		$params = array('http' => array('method' => 'POST','content' => $data));
		if ($optional_headers !== null)
		{
			$params['http']['header'] = $optional_headers;
		}

		$ctx = stream_context_create($params);
		$fp = @fopen($url, 'rb', false, $ctx);
		if (!$fp)
		{
			throw new Exception("Problem with $url, $php_errormsg");
		}
		$response = @stream_get_contents($fp);
		if ($response === false) {
			throw new Exception("Problem reading data from $url, $php_errormsg");
		}
		return $response;
	}
	
	private function do_get_request($url, $data)
	{
		$tmp = array();
		foreach($data as $param => $content)
		{
			$tmp[]= $param.'='.$content;
		}
		$param_str = implode('&', $tmp);
		$get_url = $url.'?'.$param_str;
		
		echo $get_url;
		return file_get_contents($get_url);
	}
 
}
?>